package com.api.shopapi.mapper;

import com.api.shopapi.entity.DeviceXiaoLe;
import com.api.shopapi.entity.dto.DeviceXiaoLeDTO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface DeviceXiaoLeMapper {

    @Insert("insert into device_xiaole " +
            "(" +
            "DeviceXiaoLeCode, " +
            "UserCode, " +
            "SN," +
            "ClientId," +
            "BindTime" +
            ") " +
            "VALUES " +
            "(" +
            "#{deviceXiaoLe.DeviceXiaoLeCode} ," +
            "#{deviceXiaoLe.UserCode} ," +
            "#{deviceXiaoLe.SN}, " +
            "#{deviceXiaoLe.ClientId}, " +
            "#{deviceXiaoLe.BindTime} " +
            ")")
    int insert(@Param("deviceXiaoLe") DeviceXiaoLe deviceXiaoLe);



    @Update("update device_xiaole set " +
            "UserCode=#{deviceXiaoLe.UserCode} ," +
            "SN=#{deviceXiaoLe.SN}, " +
            "ClientId=#{deviceXiaoLe.ClientId}, " +
            "BindTime=#{deviceXiaoLe.BindTime} " +
            "where " +
            "DeviceXiaoLeCode=#{deviceXiaoLe.DeviceXiaoLeCode}  ")
    int update(@Param("deviceXiaoLe") DeviceXiaoLe deviceXiaoLe);

    @Select("select * from device_xiaole where DeviceXiaoLeCode=#{deviceXiaoLeCode} ")
    DeviceXiaoLe getByDeviceXiaoLeCode(@Param("deviceXiaoLeCode") String deviceXiaoLeCode);


    @Select("select * from device_xiaole where UserCode=#{userCode}  limit 1  ")
    DeviceXiaoLe getByUserCode(@Param("userCode") String userCode);


    @Select("select  * from  device_xiaole where SN=#{sn} limit 1 ")
    DeviceXiaoLe getBySn(@Param("sn") String sn);


    @Select("select  * from  device_xiaole where SN is not null and ClientId is not null")
    List<DeviceXiaoLe> getBindingPage();



    //解绑
    @Delete("delete from device_xiaole where UserCode=#{userCode} ")
    int deleteByUserCode(@Param("userCode") String userCode);

    @Select("select  * from  device_xiaole where ClientId=#{cid} limit 1")
    DeviceXiaoLe getByClientId(@Param("cid") String cid);


    @Select("SELECT d.*,u.`Name` ElderlyName FROM device_xiaole d, `user` u WHERE u.GovernmentCode =#{governmentCode} " +
            "AND u.UserCode = d.UserCode " +
            "AND d.SN IS NOT NULL " +
            "AND d.SN!='' " +
            "AND d.ClientId IS NOT NULL " +
            "AND d.ClientId !=''")
    List<DeviceXiaoLeDTO> getListByGovernmentCode(@Param("governmentCode") String governmentCode);


    @Select("<script>" +
            "select dx.*,u.Name as ElderlyName,u.Phone  from device_xiaole dx left join user u on dx.UserCode = u.UserCode " +
            "left join user_device ud on ud.UserCode = dx.UserCode " +
            "where dx.SN is not null " +
            "and ud.XiaoLe = 1 " +
            "<if test ='phone !=null and phone!=\"\" '>" +
            " and u.Phone like concat('%',#{phone},'%') " +
            "</if>" +
            "<if test ='sn !=null and sn!=\"\" '>" +
            " and dx.SN =#{sn} " +
            "</if>" +
            "</script>")
    List<DeviceXiaoLeDTO> getPage(@Param("phone") String phone,@Param("sn") String sn);
}
